package com.bj58.data.emailreports.controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bj58.data.emailreports.model.SeoWeekVo;
import com.bj58.data.emailreports.service.SeoWeekReportService;
import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.utils.EmailReportUtil;
import com.bj58.data.emailreports.utils.MailUtil;
import com.bj58.data.emailreports.utils.ResourcesUtil;
import com.bj58.data.emailreports.utils.SortUtil;
import com.bj58.data.emailreports.utils.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.PathUtil;

/**SEO 周报
 * @author huangliang
 *
 */
public class SEOWeekReportController {
	private SeoWeekReportService seoWeekReportService= ServiceLocator.get(SeoWeekReportService.class);
	private EmailReportUtil eru = new EmailReportUtil();
	
	
	
	public String seoWeekReport(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/SeoWeekReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");//查询日期（上周）
			String lastMonth = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 14),"yyyy-MM-dd");//查询日期（上月） //9月数据表未出 先随便占位
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(lastWeek);dateList.add(lastMonth);
			
			
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
			showCols.clear();
			showCols.add("uv");
			showCols.add("seoUv");
			showCols.add("seoRate");
			
			List<String> percentList = new ArrayList<String>();//百分比指标集
			percentList.add("seoRate");
		
			
			
			String pcTable = "t_netflow_pc_pv_";  //t_netflow_pc_pv_   PC表
			String mTable = "t_netflow_m_pv_";  	//t_netflow_m_pv_   M表
			
			
			HashMap<String,List<String>>  channelTotalMap = new HashMap<String, List<String>>();
			
			//总体-PC
			List<SeoWeekVo> allPcList = seoWeekReportService.searchData(dateList, pcTable, 1);
			if(allPcList.isEmpty() || allPcList.get(0).getUv() == 0 ){
				System.out.println(" all - pc  error!");
				return "";
			}
			List<String> allPcDatas =  eru.conformDataTypeFour(allPcList, SeoWeekVo.class, "product", showCols, "statDate", percentList, dateList);
			allPcDatas = SortUtil.sortListByFileName(allPcDatas, 1, 2);
			channelTotalMap.put("总体-PC", allPcDatas);
			
			
			//总体-M
			List<SeoWeekVo> allMList = seoWeekReportService.searchData(dateList, mTable, 1);
			if(allMList.isEmpty() || allMList.get(0).getUv() == 0 ){
				System.out.println(" all - m   error!");
				return "";
			}
			List<String> allMDatas=  eru.conformDataTypeFour(allMList, SeoWeekVo.class, "product", showCols, "statDate", percentList, dateList);
			allMDatas = SortUtil.sortListByFileName(allMDatas, 1, 2);
			channelTotalMap.put("总体-M", allMDatas);
			
			
			//中小城市-PC
			List<SeoWeekVo> zxPcList = seoWeekReportService.searchData(dateList, pcTable, 2);
			if(zxPcList.isEmpty() || zxPcList.get(0).getUv() == 0 ){
				System.out.println(" zx - pc  error!");
				return "";
			}
			List<String> zxPcDatas =  eru.conformDataTypeFour(zxPcList, SeoWeekVo.class, "product", showCols, "statDate", percentList, dateList);
			zxPcDatas = SortUtil.sortListByFileName(zxPcDatas, 1, 2);
			channelTotalMap.put("中小城市-PC", zxPcDatas);
			
			
			//中小城市-M
			List<SeoWeekVo> zxMList = seoWeekReportService.searchData(dateList, mTable, 2);
			if(zxMList.isEmpty() || zxMList.get(0).getUv() == 0 ){
				System.out.println(" zx - m  error!");
				return "";
			}
			List<String> zxMDatas =  eru.conformDataTypeFour(zxMList, SeoWeekVo.class, "product", showCols, "statDate", percentList, dateList);
			zxMDatas = SortUtil.sortListByFileName(zxMDatas, 1, 2);
			channelTotalMap.put("中小城市-M", zxMDatas);
			
			
			conMap.put("channelTotalMap",channelTotalMap);
			
//			conMap.put("today", today.replaceAll("-", "."));
//			conMap.put("lastWeek", lastWeek.replaceAll("-", "."));
//			conMap.put("lastMonth", lastMonth.replaceAll("-", "."));
			
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	
	public static void main(String[] args) {
		SEOWeekReportController  wk = new SEOWeekReportController();
		String searchDate = "2015-12-01";
		
		String beginDate = DateUtil.getStringDate(DateUtil.getDateBefore(DateUtil.strToDate(searchDate, "yyyy-MM-dd"), 7),"yyyy-MM-dd");
		String endDate = DateUtil.getStringDate(DateUtil.getDateBefore(DateUtil.strToDate(searchDate, "yyyy-MM-dd"), 1),"yyyy-MM-dd");
		
		
		String context = "";
		context = wk.seoWeekReport(endDate);
		if(!"".equals(context)){
			MailUtil.javaMailSend("SEO周报测试 from " + beginDate + " to " + endDate, context, "guoxue@58.com",null);
		}
		
	}
}
